<?php

$watermark=1; //是否附加水印(1为加水印,其他为不加水印);
$watertype=2; //水印类型(1为文字,2为图片)
$waterstring="http://www.168hs.cn/"; //水印字符串
$waterimg="http://localhost:83/images/mark.gif"; //水印图片，试了相对路不行，只好用绝对路径。

if($watermark==1)
{
$myfilename="../../../attached/".$ymd."/".$new_file_name; 
$iinfo=getimagesize($myfilename,$iinfo); 
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($myfilename);
break;
case 2:
$simage =imagecreatefromjpeg($myfilename);
break;
case 3:
$simage =imagecreatefrompng($myfilename);
break;
case 6:
$simage =imagecreatefromwbmp($myfilename);
break;
default:
die("不支持的文件类型");
exit;
}

imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);

switch($watertype)
{
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印图片
$simage1 =imagecreatefromgif($waterimg);
imagecopymerge($nimage,$simage1,$image_size[0]-150,$image_size[1]-50,0,0,150,50,60); //设置水印的位置 
imagedestroy($simage1);
break;
}

switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $myfilename);
break;
case 2:
imagejpeg($nimage, $myfilename);
break;
case 3:
imagepng($nimage, $myfilename);
break;
case 6:
imagewbmp($nimage,$myfilename);
//imagejpeg($nimage, $destination);
break;
}

//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}
?>